package cn.ococci.dm.Main.View;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.ococci.dm.Main.Model.Ota;
import cn.ococci.dm.Main.Tools.StringTool;
import cn.ococci.dm.Main.Tools.ThreadTool;
import cn.ococci_dm.R;

/**
 * Created by cyan on 16/1/22.
 * 升级视图
 */
public class OTAUpgradeView extends LinearLayout {

    private TextView apVersionTv;
    private TextView prVersionTv;

    private ImageView apImageView;
    private ImageView prImageView;

    private Button upGradeBtn;

    private TranslateAnimation enterAnim,exitAnim;

    private OtaOperator otaOperator;

    public interface  OtaOperator{

        void clickUpgrade();

    }

    public void setOtaOperator(OtaOperator otaOperator){
        this.otaOperator = otaOperator;
    }


    public OTAUpgradeView(Context context) {
        this(context,null);
    }

    public OTAUpgradeView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }


    public OTAUpgradeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }



    private void initView(Context context){
        View view =  LayoutInflater.from(context).inflate(R.layout.ota_upgrade,this,true);

        apVersionTv = (TextView) view.findViewById(R.id.apVersionTv);
        prVersionTv = (TextView) view.findViewById(R.id.prVersionTv);
        apImageView = (ImageView) view.findViewById(R.id.apImageView);
        prImageView = (ImageView) view.findViewById(R.id.prImageView);
        upGradeBtn = (Button) view.findViewById(R.id.upGradeBtn);

        enterAnim = new TranslateAnimation( Animation.RELATIVE_TO_SELF,  0,  Animation.RELATIVE_TO_SELF,  0,
                Animation.RELATIVE_TO_SELF, -1,  Animation.RELATIVE_TO_SELF,  0);
        enterAnim.setDuration(600);

        exitAnim = new TranslateAnimation( Animation.RELATIVE_TO_SELF,  0,  Animation.RELATIVE_TO_SELF,  0,
                Animation.RELATIVE_TO_SELF, 0,  Animation.RELATIVE_TO_SELF,  -1);
        exitAnim.setDuration(600);
        exitAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                setVisibility(GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        initListener();
    }


    /**
     *  初始化数据
     * @param versions
     * @param otaList
     */
    public void initUpgradeView(String[] versions,Ota[] otaList){

        this.setVisibility(VISIBLE);

        if(StringTool.compareVersion(otaList[0].getVersion(),versions[0])){
            apImageView.setVisibility(VISIBLE);
        }else {
            apImageView.setVisibility(GONE);
        }
        apVersionTv.setText(otaList[0].getVersion());


        if(StringTool.compareVersion(otaList[1].getVersion(),versions[1])){
            prImageView.setVisibility(VISIBLE);
        }else {
            prImageView.setVisibility(GONE);
        }

        prVersionTv.setText(otaList[1].getVersion());

    }


    public void hiddenPRVersionStatus(final String version){

        ThreadTool.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                prImageView.setVisibility(VISIBLE);
                prVersionTv.setText(version);
            }
        });

    }



    private void initListener(){

        upGradeBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                otaOperator.clickUpgrade();
            }
        });

    }


    public void showUpgradeView(){

        if(getVisibility() == View.GONE){
            setVisibility(View.VISIBLE);

        }

    }


    public void hiddenUpgradeView(){

        if(getVisibility() == View.VISIBLE){
            setVisibility(View.GONE);
        }
    }



}
